package com.yingxin.yxerp.module.inform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yingxin.yx.framework.commons.dto.PageOut;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yx.framework.mybatisplus.PageHelper;
import com.yingxin.yxerp.module.inform.entity.InformerSet;
import com.yingxin.yxerp.module.inform.entity.builder.InformerSetBuilder;
import com.yingxin.yxerp.module.inform.service.IInformerSetService;
import com.yingxin.yxerp.module.inform.vo.req.InformerSetCreateVo;
import com.yingxin.yxerp.module.inform.vo.req.InformerSetQueryVo;
import com.yingxin.yxerp.module.inform.vo.req.InformerSetUpdateVo;
import com.yingxin.yxerp.module.inform.vo.resp.InformerSetRespVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 通知人设置 前端控制器
 * @author dongpengfei
 * @since 2021-12-10
 */

@RestController
@RequestMapping("/informer/set")
public class InformerSetController {

    @Autowired
    private IInformerSetService informerSetService;

    @ApiOperation(value = "查询列表（分页）")
    @PostMapping("/page")
    public PageOut<InformerSetRespVo> page(@RequestBody InformerSetQueryVo queryVo) {
        IPage<InformerSetRespVo> page = informerSetService.pageVo(PageHelper.in(queryVo), queryVo);
        return PageHelper.out(page);
    }

    @ApiOperation(value = "查询列表（全部）")
    @PostMapping("/list")
    public List<InformerSetRespVo> list(@RequestBody InformerSetQueryVo queryVo) {
        return informerSetService.listVo(queryVo);
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public InformerSetRespVo get(@PathVariable("id") String id) {
        return informerSetService.getVo(id);
    }

    @PreAuthorize("hasAuthority('informer:setting:add')")
    @ApiOperation(value = "新增")
    @PostMapping
    public String create(@RequestBody @Valid InformerSetCreateVo createVo) {
        int count = informerSetService.countByTypeName(null, createVo.getInformType());
        if (count > 0) {
            throw new BizException("该通知类型已存在");
        }
        InformerSet InformerSet = InformerSetBuilder.build(createVo);
        informerSetService.save(InformerSet);
        return InformerSet.getId();
    }

    @PreAuthorize("hasAuthority('informer:setting:update')")
    @ApiOperation(value = "修改")
    @PutMapping
    public String update(@RequestBody @Valid InformerSetUpdateVo updateVo) {
        int count = informerSetService.countByTypeName(updateVo.getId(), updateVo.getInformType());
        if (count > 0) {
            throw new BizException("该通知类型已存在");
        }
        InformerSet InformerSet = InformerSetBuilder.build(updateVo);
        informerSetService.updateById(InformerSet);
        return InformerSet.getId();
    }

    @PreAuthorize("hasAuthority('informer:setting:delete')")
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        informerSetService.removeById(id);
    }

}
